home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / contrib / russo / gplot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-25  |  2.6 KB  |  105 lines

  1. /*
  2.  * gplot.c - A demo driver for the gnuplotio library.
  3.  *
  4.  * Nov 28, 1991
  5.  *
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <ctype.h>
  10. #include "gnuplotio.h"
  11. #include "util.h"        /* print_nonprint() */
  12.  
  13. void main(int argc, char *argv[])
  14. {
  15. GNUPLOT *gp;
  16. #define N 10
  17. char buf[N];
  18. int curve,points;
  19. int s;
  20. float *x,*y,*z;
  21. char *range;
  22. int i;
  23. GPCurve *gc;
  24.  
  25. /* Open gnuplot connection */
  26.     if ((gp = openGnuplot(argv[1])) == NULL)
  27.     {
  28.         fprintf(stderr,"openGnuplot failed :(\n");
  29.         exit(EXIT_FAILURE);
  30.     }
  31.  
  32. /* Enter evaluator mode */
  33.     s=writeGnuplot(gp, "set term table\n");
  34.     if (s == 0) fprintf(stderr,"err writeGnuplot\n");
  35.     s=writeGnuplot(gp, "set samples 10\n");
  36.     if (s == 0) fprintf(stderr,"err writeGnuplot\n");
  37.  
  38. /* Write a bogus command to test error reporting */
  39.     s=writeGnuplot(gp, "pluck\n");
  40.     s=readErrorGnuplot(gp, buf, sizeof(buf));
  41.     if(s == -1) fprintf(stderr,"err readError\n");
  42.     else if (s > 0)
  43.     {  printf("Error buf (n=%d):\n",s);
  44.        print_nonprint(buf,HOW_ESCAPE);
  45.        print_nonprint(buf,HOW_OCTAL);
  46.        print_nonprint(buf,HOW_HEX);
  47.        print_nonprint(buf,HOW_QUES);
  48.     }
  49.  
  50. /* Send a plot command */
  51.     s=writeGnuplot(gp, "plot sin(x)");
  52.     if (s == 0) fprintf(stderr,"err writeGnuplot 2\n");
  53.  
  54. /* Read curve piecemeal */
  55.     s=readCurveHeadGnuplot(gp, &curve, &points);
  56.     if (s == 0) fprintf(stderr,"err readCurveHead\n");
  57.  
  58.     printf("Curve %d has %d points\n",curve,points);
  59. /* Alloc buffers */
  60.     x = (float *) malloc(sizeof(float) * (size_t)points);
  61.     y = (float *) malloc(sizeof(float) * (size_t)points);
  62.     range = (char *) malloc(sizeof(char) * (size_t)points);
  63.  
  64. /* Read 2D data */
  65.     s=readCurve2Gnuplot(gp, NULL, x,y,points);
  66.     if (s == 0) fprintf(stderr,"err readCurve2\n");
  67.  
  68. /* Dump data */
  69.     for (i=0; i<points; i++)
  70.         printf("%g  %g\n",x[i],y[i]);
  71.  
  72. /* Check in for any error messages */
  73.     s=readErrorGnuplot(gp, buf, sizeof(buf));
  74.     if(s == -1) fprintf(stderr,"err readError\n");
  75.     else if (s > 0) /*printf("Error buf (n=%d): \"%s\"\n",s,buf);*/
  76.     {  printf("Error buf (n=%d):\n",s);
  77.        print_nonprint(buf,HOW_ESCAPE);
  78.     }
  79.  
  80. /* Now do a 3D plot */
  81.     s=writeGnuplot(gp, "splot sin(x)**2");
  82.     if (s == 0) fprintf(stderr,"err writeGnuplot 3\n");
  83.  
  84. /* But read it the easy way */
  85.     if ((gc = readCurveGnuplot(gp)) != NULL)
  86.     {
  87.         /* Dump data */
  88.         printf("curveno=%d is_2d=%d npts=%u\n",gc->curveno,gc->is_2d,
  89.            gc->npts);
  90.         for (i=0; i<gc->npts; i++)
  91.         {
  92.         if (gc->is_2d)
  93.             printf("%c %g  %g\n",gc->range[i],gc->x[i],gc->y[i]);
  94.         else
  95.             printf("%c %g  %g %g\n",gc->range[i],gc->x[i],gc->y[i],
  96.             gc->z[i]);
  97.         }
  98.     }
  99.  
  100. /* Close gnuplot connection */
  101.     s=closeGnuplot(gp,1);
  102.     if (s == 0) fprintf(stderr,"err close\n");
  103. }
  104.  
  105.